﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ConfeccionesJR.Entites;
using System.Data;
using System.Data.Objects;

namespace ConfeccionesJR.Repository
{
    public class MaterialRepository:MasterDataAccess, IMaterialRepository
    {
        public List<MATERIAL> GetAllFromMaterial()
        {
            var query = from p in Context.MATERIALs.Include("UNIDAD_MEDIDA")
                        select p;

            ObjectContext.Refresh(RefreshMode.StoreWins, query);

            return query.ToList();
        }

        public List<MATERIAL> GetFromMaterialByDescripcion(string descripcion)
        {
            if (descripcion != string.Empty)
            {
                var query = from p in Context.MATERIALs.Include("UNIDAD_MEDIDA")
                            where p.Descripcion.ToUpper().Contains(descripcion.ToUpper())
                            select p;
                
                ObjectContext.Refresh(RefreshMode.StoreWins, query);

                return query.Take(50).ToList();
            }
            else
            {
                var query1 = from p in Context.MATERIALs.Include("UNIDAD_MEDIDA")
                             select p;
                
                ObjectContext.Refresh(RefreshMode.StoreWins, query1);

                return query1.Take(50).ToList();
            }
        }

        public List<MATERIAL> GetAllFromMaterialsByDescripcionAndStockBajo(string descripcion, int minStock)
        {
            if (descripcion != string.Empty)
            {
                var query = from p in Context.MATERIALs.Include("UNIDAD_MEDIDA")
                            where (p.Descripcion.ToUpper().Contains(descripcion.ToUpper())) 
                            && p.Stock < minStock
                            select p;
                
                ObjectContext.Refresh(RefreshMode.StoreWins, query);

                return query.Take(50).ToList();
            }
            else
            {
                var query = from p in Context.MATERIALs.Include("UNIDAD_MEDIDA")
                            where (p.Descripcion.ToUpper().Contains(""))
                            && p.Stock < minStock
                            select p;
                
                ObjectContext.Refresh(RefreshMode.StoreWins, query);

                return query.Take(50).ToList();
            }
        }

        public MATERIAL GetFromMaterialById(int id)
        {
            var query = from p in Context.MATERIALs
                        where p.Id.Equals(id)
                        select p;

            ObjectContext.Refresh(RefreshMode.StoreWins, query);

            return query.SingleOrDefault();
        }

        public void AddMaterial(MATERIAL material)
        {
            Context.MATERIALs.Add(material);
            Context.SaveChanges();
        }

        public void UpdateMaterial(MATERIAL material)
        {
            Context.MATERIALs.Attach(material);
            //Context.Entry(material.UNIDAD_MEDIDA).State = EntityState.Unchanged; 
            Context.Entry(material).State = EntityState.Modified;
            Context.SaveChanges();
        }


        public MATERIAL GetOneMaterialByDescripcion(string codigo)
        {
            return Context.MATERIALs.Where(c => c.Descripcion.Equals(codigo)).FirstOrDefault();
        }

        public bool ExisteDuplicidadMatDescripcion(string MaterialDescripcion, int id)
        {
            var query = from c in Context.MATERIALs
                        where c.Descripcion.ToUpper().CompareTo(MaterialDescripcion.ToUpper()) == 0 && c.Id != id
                        select c;

            ObjectContext.Refresh(RefreshMode.StoreWins, query);

            return query.Any();
        }

    }
}
